using com.binance.client.model.trade;
using NUnit.Framework;
using QntPlatform.BinanceApi;
using QntPlatform.Strategy.S2;
using System;
using System.Diagnostics;
using static QntPlatform.BinanceApi.ApiClientBase;
using System.Linq;
using QntPlatform.Db;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using QntPlatform;
using QntSvc.Web.Controllers;
using Microsoft.Extensions.Logging.TraceSource;

namespace NUnitTestProject1
{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
            //Microsoft.Extensions.Logging.Logger<T>
            //Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider d;
            //System.Diagnostics.DelimitedListTraceListener
            TraceSourceLoggerProvider p;
            //  Assert.Pass();
        }

        [Test]
        public void dbStrueTest() {

            var mg = new DbStructManage();
           var names=    mg .GetTablesName();
          var tab=  mg.GetTableInfo("RobotInfo",1);

        }
        [Test]
        public void Test1()
        {
            var key = "AzLtCvLYr52We7otVbNABHRAMEIhx83RvKZF8hhwXeKufOqIiJMP3tSkZFZQ6MCi";// "6vyuqXVVq89B8tBC290IjjufCQnWcmniEZVLVBpuk89algzGhiq60OuQD3DHYTHz";
            var skey = "f4vYmpI4dvDw7O5JNdkDJBzgmkLv74m9xZUut3QbEKjaFXarJmuIWHeFOT7tczhi";// "y5BNJhNzTPL858yVeLd2Dfp5v6MZVgvmkMKoj08DZvjG9Ss3LxMLOZkJJud2YLG3";



            // var fc = new FApiClent();
            // fc.GetRecords(60);

            var jf = new JFApiClient(key, skey).account();
            // var rr = jf.CreateReq("/fapi/v2/account", recvWindow: 10000);
            // var re = rr
            //     .AddQueryParameter("aaaa", "1111")
            //   .Get();
            //var re2= rr.Client.Deserialize<string>(re);
            Debug.Print(jf.Assets.Count + "-------------------------------");
            var btc = jf.Positions.FirstOrDefault(p => p.Symbol == "BTCUSDT");
            Debug.Print(btc + "");
            //Debug.Print(re2.Data+ "----c");
            //      var exchange = new BinanceExchangeImp(key, skey, "BTCUSDT");
            //    var re1= exchange.GetRecordsAsync(60);
            //    var rs1= re1.Result;
            //         var re = exchange.GetAccountAsync();
            //         var rs = re.Result;
            //         Console.WriteLine(rs+"--"+rs1);

        }
        [Test]
        public void Test2()
        {
            var t = new TuPo_2();
            var arr = t.GetArgInfosByAttr();
            Console.WriteLine(arr);
        }
        [Test]
        public void TestWorkerId()
        {
            var idw = IdWorker.TimeSeqIdWorker.GetInstance(1);

            var stb = new StringBuilder();
            Task tt = null;
            for (int i = 0; i < 10; i++)
            {
                tt = Task.Run(() =>
                  {
                      var index = 1;
                      while (index < 10000 + (10 - i) * 1000)
                      {
                          stb.Append("," + idw.NextId());
                          index++;
                      }
                  });
            }
            tt.Wait();
            Console.WriteLine(stb.ToString());
        }
          [Test]
        public void ExchangeEventTest()
        {
            var exch = new BinanceExchangeImp("key1", "skey1", "BTCUSDT");
            exch.TickerChangeEvent += (p1, p2) =>
            {
                    Console.WriteLine(p2.Buy);
            };
        
            System.Threading.Thread.Sleep(15000);
        }

        [Test]
        public void LogTest()
        {
            var list = new List<LogInfo>();
            for (int i = 0; i < 500; i++)
            {
                var info = new LogInfo();
                info.OwnerId = i % 9;
                info.LogType = "Info";
                info.Info1 = "test-" + i;
                info.Info2 = i + "";
                list.Add(info);
            }

            var t1 = Task.Run(() =>
              {
                  for (int i = 0; i < 300; i++)
                  {
                      LogStore.Insert(new[] { list[i] });
                  }

              });
            Task.Run(() =>
            {
                for (int i = 300; i < 500; i++)
                {
                    LogStore.Insert(new[] { list[i] });
                }
            });
            t1.Wait();
        }
        [Test]
        public void TestOrderLog(){
            var Exchange=new BinanceExchangeImp("qqq","qqq","qqq");
            
            var id=Exchange.CreateOrder(0.123m,SideDirection.Buy, info: new QntPlatform.Db.LogInfo() {  ParentActId= "p123",Info2="止损,市价:0.111"}).Result;
            Console.WriteLine(id);
        }
        public void TestLogSelect()
        {
           var select=new LogController();
           //select.GetRobotLogs(null,null,)
           
        }
    }
}
public class Rootobject
{
    public int feeTier { get; set; }
    public bool canTrade { get; set; }
    public bool canDeposit { get; set; }
    public bool canWithdraw { get; set; }
    public int updateTime { get; set; }
    public string totalInitialMargin { get; set; }
    public string totalMaintMargin { get; set; }
    public string totalWalletBalance { get; set; }
    public string totalUnrealizedProfit { get; set; }
    public string totalMarginBalance { get; set; }
    public string totalPositionInitialMargin { get; set; }
    public string totalOpenOrderInitialMargin { get; set; }
    public string totalCrossWalletBalance { get; set; }
    public string totalCrossUnPnl { get; set; }
    public string availableBalance { get; set; }
    public string maxWithdrawAmount { get; set; }
    public Asset[] assets { get; set; }
    public Position[] positions { get; set; }
}

public class Asset
{
    public string asset { get; set; }
    public string walletBalance { get; set; }
    public string unrealizedProfit { get; set; }
    public string marginBalance { get; set; }
    public string maintMargin { get; set; }
    public string initialMargin { get; set; }
    public string positionInitialMargin { get; set; }
    public string openOrderInitialMargin { get; set; }
    public string maxWithdrawAmount { get; set; }
    public string crossWalletBalance { get; set; }
    public string crossUnPnl { get; set; }
    public string availableBalance { get; set; }
}

public class Position
{
    public string symbol { get; set; }
    public string initialMargin { get; set; }
    public string maintMargin { get; set; }
    public string unrealizedProfit { get; set; }
    public string positionInitialMargin { get; set; }
    public string openOrderInitialMargin { get; set; }
    public string leverage { get; set; }
    public bool isolated { get; set; }
    public string entryPrice { get; set; }
    public string maxNotional { get; set; }
    public string positionSide { get; set; }
    public string positionAmt { get; set; }
    public string notional { get; set; }
    public string isolatedWallet { get; set; }
}
